﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace CUI.CUIOption
{
    public class OptionItems
    {
        public CMATUIType kind { get; set; }
        public Dictionary<string, string> data;
        public bool hasNext { get; set; }
        public Dictionary<string, string> attribute { get; set; }
        public string tip { get; set; }
        public bool disableCopy { get; set; }
        public string right { get; set; }

        public OptionItems()
        {
            kind = CMATUIType.unkonw;
            data = new Dictionary<string, string>();
            attribute = new Dictionary<string, string>();
        }
    }


    public static class OptionOptionItemsHelp
    {
        public static KeyValuePair<string, List<OptionItems>> XMLToOptionItems(XElement option)
        {
            var returnValueList = new List<OptionItems>();
            string id = string.Empty;
            foreach (var attr in option.Attributes())
            {
                switch (attr.Name.LocalName)
                {
                    case "id":
                        id = attr.Value;
                        break;
                    case "right":
                        //To do filter here by right list
                        break;
                }
            }

            foreach (var item in option.Elements())
            {
                var returnValue = new OptionItems();
                foreach (var element in item.Elements())
                {
                    switch (element.Name.LocalName)
                    {
                        case "datas":
                            foreach (var data in element.Elements())
                            {
                                returnValue.data.Add(data.Attribute(XName.Get("key")).Value, data.Value);
                            }
                            break;
                        case "attributes":
                            foreach (var attr in element.Elements())
                            {
                                returnValue.attribute.Add(attr.Attribute(XName.Get("key")).Value, attr.Value);
                            }
                            break;
                        case "hasNext":
                            returnValue.hasNext = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
                            break;
                        case "type":
                            switch (element.Value)
                            {
                                case "cDropdownList":
                                    returnValue.kind = CMATUIType.cDropdownList; break;
                                case "cTextbox":
                                    returnValue.kind = CMATUIType.cTextbox; break;
                                case "cCheckbox":
                                    returnValue.kind = CMATUIType.cCheckbox; break;
                                case "cRadioGroup":
                                    returnValue.kind = CMATUIType.cRadioGroup; break;
                                case "cTextarea":
                                    returnValue.kind = CMATUIType.cTextarea; break;
                                case "cButton":
                                    returnValue.kind = CMATUIType.cButton; break;
                                default:
                                    returnValue.kind = CMATUIType.unkonw; break;
                            }
                            break;
                    }
                }
                returnValueList.Add(returnValue);
            }
            return new KeyValuePair<string, List<OptionItems>>(id, returnValueList);
        }
    }

}